package com.racetime.controller;

import com.racetime.util.MyLogger;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.logging.Logger;

/**
 * @author : Ice'Clean
 * @date : 2021-05-21
 */
@WebServlet(name = "BaseServlet", value = "/BaseServlet")
public class BaseServlet extends HttpServlet {
    Logger baseLogger = MyLogger.getLogger("baseLogger");

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

        try {
            // 获取得到目标方法并执行
            String methodName = request.getParameter("method");
            baseLogger.info("收到的方法名：" + methodName);
            Class<? extends BaseServlet> actionClass = this.getClass();
            Method method = actionClass.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            baseLogger.info("收到的方法：" + method);
            method.invoke(this, request, response);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        baseLogger.info("收到 post 请求！");
        baseLogger.info("用户名：" + request.getParameter("userName") + " , " + "密码：" + request.getParameter("userPass"));
        doGet(request, response);
    }
}
